import java.util.ArrayList;
import java.util.List;

/**
 * 78. 子集
 * https://leetcode.cn/problems/subsets/description/
 */
class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    public List<List<Integer>> subsets(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();

        dfs(nums, 0);
        
        return ret;
    }
    //解法1
    void dfs(int[] nums, int pos) {
        if(pos == nums.length) {
            ret.add(new ArrayList<>(path));
            return;
        }
        //选
        path.add(nums[pos]);
        dfs(nums, pos + 1);
        //不选
        path.remove(path.size() - 1);//恢复现场
        dfs(nums, pos + 1);
    }
    //解法2
    void dfs2(int[] nums, int pos) {
        ret.add(new ArrayList(path));
        for(int i = pos; i < nums.length; i++) {
            path.add(nums[i]);
            dfs(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
}